var MyXMLHttpRequest = function() {
	if (window.XMLHttpRequest) {
		this.xmlhttp = new XMLHttpRequest();
		if (this.xmlhttp.overrideMimeType) {
			this.xmlhttp.overrideMimeType("text/xml");
		}
	} else if (window.ActiveXObject) {
		var microsoft = ['Micosoft.XMLHTTP', 'MSXML2.XMLHTTP',
				'MSXML2.XMLHTTP.5.0', 'MSXML2.XMLHTTP.4.0',
				'MSXML2.XMLHTTP.3.0'];
		for (var i = 0; i < microsoft.length; i++) {
			try {
				this.xmlhttp = new ActiveXObject(microsoft[i]);
				break;
			} catch (e) {
				alert(e.description);
			}
		}
	} else {
		this.xmlhttp = null;
	}
}

MyXMLHttpRequest.prototype.send = function(method, url, data, dataType, callback) {
	var xmlhttp = this.xmlhttp;
	this.xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				callback(xmlhttp, dataType);
			}
		}
	};
	if (method == "POST") {
		this.xmlhttp.open(method, url, true);
		this.xmlhttp.setRequestHeader("Content-Type",
				"application/x-www-form-urlencoded");
		this.xmlhttp.send(data + "&datatype=" + dataType);
	} else {
		this.xmlhttp.open(method, url + "&datatype=" + dataType, true);
		this.xmlhttp.send(null);
	}

}

function parseMessage(xmlhttp, dataType) {
	var flag;
	var info;
	if (dataType == "xml") {
		var xmlDoc = xmlhttp.responseXML.documentElement;
		var result = xmlDoc.getElementsByTagName('result');
		var node = xmlDoc.getElementsByTagName('info');
		flag = (result && result[0].firstChild.nodeValue == "true") ? true : false;
		info = node[0].firstChild.nodeValue;
		
	} else if (dataType == "text") {
		var txtDoc = xmlhttp.responseText;
		var result = txtDoc.split("$$");
		flag = (result[0] && result[0] == "true") ? true : false;
		info = result[1];
	} else if (dataType == "json") {
		var txtDoc = xmlhttp.responseText;
		var obj = eval("(" +txtDoc + ")");
		flag = obj.result;
		info = obj.info;
	}
	$('checkmsg').style.color = flag ? "green" : "red"; 
	$('checkmsg').innerHTML = $('username').value + " " + info;
}

function checkUsername() {
	var username = $('username').value;
	if (username.trim() == "") {
		$('checkmsg').style.color = "red"; 
		$('checkmsg').innerHTML = "Username is required!"
		return;
	}
	var xmlHtml = new MyXMLHttpRequest();
	//xmlHtml.send("GET", "/servlet/CheckServlet?username=" +  username.trim(), null, "xml", parseMessage);
	//xmlHtml.send("GET", "/servlet/CheckServlet?username=" + username.trim(), null, "html", parseMessage);
	//xmlHtml.send("GET", "/servlet/CheckServlet?username=" + username.trim(), null, "json", parseMessage);
	//xmlHtml.send("POST", "/servlet/CheckServlet", "username=" + username.trim(), "xml",  parseMessage)
	//xmlHtml.send("POST", "/servlet/CheckServlet", "username=" + username.trim(), "html",  parseMessage)
	xmlHtml.send("POST", "/servlet/CheckServlet", "username=" + username.trim(), "json",  parseMessage)
	//java.io.CharConversionException: isHexDigit escape
}

function getXmlHttpRequest() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}

String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

function $(id) {
	return document.getElementById(id);
}